﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PersonProject;
using System.Threading;
using System.Globalization;
using System.IO;

namespace Person_Form_Socket
{
    public partial class EditForm : Form
    {
        public string command;
        public int id;
        Person p;
        PersonAppSocket mainform;
        public EditForm(PersonAppSocket f)
        {
            mainform = f;
            InitializeComponent();
        }

        public void CheckAndFill()
        {
            foreach (Person pp in PersonAppSocket.pList)
            {
                if (pp != null)
                {
                    if (pp.Id == id)
                    {
                        p = pp;
                        break;
                    }
                }
            }
            TextBox_id.Text = p.Id.ToString();
            TextBox_name.Text = p.FName;
            TextBox_add1.Text = p.LName;
            TextBox_add2.Text = p.Age.ToString();
            TextBox_Memo.Text = Encoding.UTF8.GetString(p.Memo);
            DataGrid_Contact.DataSource = p.Contacts;
            DataGrid_Adress.DataSource = p.Adresses;

            //assign image from binary
            MemoryStream ms = new MemoryStream(p.Image);
            PersonPicture.Image = Bitmap.FromStream(ms);
        }

        public void EmptyFill()
        {
            p = new Person(0);
            TextBox_id.Text = "0";
            TextBox_name.Text = "";
            TextBox_add1.Text = "";
            TextBox_add2.Text = "";
            DataGrid_Contact.DataSource = p.Contacts;
            DataGrid_Adress.DataSource = p.Adresses;
        }

        private void Cancel_btn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Okay_btn_Click(object sender, EventArgs e)
        {
            p.Id = Convert.ToInt32(TextBox_id.Text, CultureInfo.CurrentCulture);
            p.FName = TextBox_name.Text;
            p.LName = TextBox_add1.Text;
            p.Age = Convert.ToInt32(TextBox_add2.Text, CultureInfo.CurrentCulture);
            p.Memo = Encoding.UTF8.GetBytes(TextBox_Memo.Text);

            PersonAppSocket.ds.Update(p);

            mainform.Load_Btn_Click(this, new EventArgs());
            this.Close();

        }

        private void BtnAddContact_Click(object sender, EventArgs e)
        {
            Button Btn = (Button)sender;
            string BtnName = Btn.Name;

            switch (BtnName)
            {
                case "BtnAddContact": p.Contacts.Add(new Contact());
                    break;
                case "BtnAddAdress": p.Adresses.Add(new Adress());
                    break;
                case "BtnDeleteContact":
                    if (DataGrid_Contact.SelectedCells.Count > 0)
                    {
                        Contact selected = (Contact)DataGrid_Contact.SelectedCells[0].OwningRow.DataBoundItem;
                        p.Contacts.Remove(selected);
                    }
                    break;
                case "BtnDeleteAdress":
                    if (DataGrid_Adress.SelectedCells.Count > 0)
                    {
                        Adress selected = (Adress)DataGrid_Adress.SelectedCells[0].OwningRow.DataBoundItem;
                        p.Adresses.Remove(selected);
                    }
                    break;
                default: break;
            }

            DataGrid_Contact.DataSource = null;
            DataGrid_Contact.DataSource = p.Contacts;
            DataGrid_Adress.DataSource = null;
            DataGrid_Adress.DataSource = p.Adresses;
            DataGrid_Contact.EndEdit();
            DataGrid_Contact.Refresh();
            DataGrid_Contact.Invalidate();
        }



        private void PersonPicture_Click(object sender, EventArgs e)
        {
            OpenFileDialog OFDialog = new OpenFileDialog();
            OFDialog.Filter = "Image Files | *.bmp; *.gif; *.tif; *.jpg; *.png; ";
            DialogResult result = OFDialog.ShowDialog();
            if (result == DialogResult.OK) 
            {
                PersonPicture.Image = Bitmap.FromFile(OFDialog.FileName);
                p.Image = File.ReadAllBytes(OFDialog.FileName);
            }
        }
    }
}
